<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('clock_in_rules', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 规则基本信息
            $table->string('name', 100);
            $table->string('code', 50)->unique();
            $table->text('description')->nullable();

            // 打卡时间设置
            $table->time('morning_start_time')->nullable(); // 上午上班时间
            $table->time('morning_end_time')->nullable(); // 上午下班时间
            $table->time('afternoon_start_time')->nullable(); // 下午上班时间
            $table->time('afternoon_end_time')->nullable(); // 下午下班时间
            $table->time('evening_start_time')->nullable(); // 晚上上班时间
            $table->time('evening_end_time')->nullable(); // 晚上下班时间

            // 弹性时间设置
            $table->integer('flexible_minutes')->default(30); // 弹性打卡分钟数
            $table->boolean('allow_early_clock_in')->default(true); // 允许提前打卡
            $table->boolean('allow_late_clock_out')->default(true); // 允许延迟打卡

            // 打卡类型配置
            $table->boolean('require_morning_clock_in')->default(true);
            $table->boolean('require_morning_clock_out')->default(true);
            $table->boolean('require_afternoon_clock_in')->default(true);
            $table->boolean('require_afternoon_clock_out')->default(true);
            $table->boolean('require_evening_clock_in')->default(false);
            $table->boolean('require_evening_clock_out')->default(false);

            // 位置验证设置
            $table->boolean('require_location_validation')->default(false);
            $table->boolean('require_photo_verification')->default(false);
            $table->boolean('allow_remote_clock_in')->default(false);

            // 地理围栏设置
            $table->boolean('use_geofence')->default(false);
            $table->json('default_geofences')->nullable(); // 默认地理围栏ID列表
            $table->integer('location_tolerance_meters')->default(100); // 位置容差（米）

            // 打卡设备限制
            $table->boolean('restrict_device')->default(false);
            $table->json('allowed_devices')->nullable(); // 允许的设备列表
            $table->boolean('allow_mobile_clock_in')->default(true);
            $table->boolean('allow_web_clock_in')->default(true);

            // 补卡设置
            $table->boolean('allow_makeup_clock')->default(true);
            $table->integer('makeup_deadline_hours')->default(24); // 补卡截止小时数
            $table->integer('max_makeup_count_per_month')->default(3);
            $table->boolean('require_makeup_approval')->default(true);

            // 异常处理
            $table->boolean('auto_detect_exceptions')->default(true);
            $table->json('exception_notification_rules')->nullable(); // 异常通知规则

            // 特殊日期设置
            $table->json('holiday_rules')->nullable(); // 节假日规则
            $table->json('weekend_rules')->nullable(); // 周末规则
            $table->boolean('work_on_holidays')->default(false);

            // 状态和有效期
            $table->string('status', 20)->default('active');
            $table->date('effective_date');
            $table->date('expiration_date')->nullable();
            $table->boolean('is_default')->default(false);

            // 适用范围
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id')->nullable();
            $table->string('position_name', 100)->nullable();
            $table->json('applicable_employees')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['status']);
            $table->index(['is_default']);
            $table->index(['effective_date', 'expiration_date']);
            $table->index(['department_id']);
            $table->index(['position_id']);
            $table->index(['code']);

            // 外键约束
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('clock_in_rules');
    }
};
